<template>
  <div class="app-container">
    <el-form :model="queryParams" class="top" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="昵称" prop="touristNickName">
        <el-input
          v-model="queryParams.touristNickName"
          placeholder="请输入昵称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="性别" prop="touristSex">
        <el-select v-model="queryParams.touristSex" placeholder="请选择性别" clearable>
          <el-option
            v-for="dict in dict.type.sys_user_sex"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="出生年份" prop="touristOld">
        <el-input
          v-model="queryParams.touristOld"
          placeholder="请输入出生年份"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="类型" prop="touristType">
        <el-select v-model="queryParams.touristType" placeholder="请选择类型" clearable>
          <el-option
            v-for="dict in dict.type.sys_tourist_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="来源" prop="touristArea">
        <el-select v-model="queryParams.touristArea" placeholder="请选择来源" clearable>
          <el-option
            v-for="dict in dict.type.sys_tourist_area"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="手机号码" prop="touristPhonenumber">
        <el-input
          v-model="queryParams.touristPhonenumber"
          placeholder="请输入手机号码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['scenery:tourist:add']"
        >新增</el-button>
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['scenery:tourist:edit']"
        >修改</el-button>
          <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['scenery:tourist:remove']"
          >删除</el-button>
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['scenery:tourist:export']"
        >导出</el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" :data="touristList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="touristId" />
      <el-table-column label="昵称" align="center" prop="touristNickName" />
      <el-table-column label="性别" align="center" prop="touristSex">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_user_sex" :value="scope.row.touristSex"/>
        </template>
      </el-table-column>
      <el-table-column label="出生年份" align="center" prop="touristOld" />
      <el-table-column label="类型" align="center" prop="touristType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_tourist_type" :value="scope.row.touristType"/>
        </template>
      </el-table-column>
      <el-table-column label="来源" align="center" prop="touristArea">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_tourist_area" :value="scope.row.touristArea"/>
        </template>
      </el-table-column>
      <el-table-column label="爱好类型" align="center" prop="touristHobby">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_tourist_hobby" :value="scope.row.touristHobby ? scope.row.touristHobby.split(',') : []"/>
        </template>
      </el-table-column>
      <el-table-column label="手机号码" align="center" prop="touristPhonenumber" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['scenery:tourist:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['scenery:tourist:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!--添加图表-->
    <div>
      <div id="Sex_ratio" style="display: flex;
    width: 350px; height: 350px;
    box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1)"></div>
    </div>
    <!-- 添加或修改游客管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="昵称" prop="touristNickName">
          <el-input v-model="form.touristNickName" placeholder="请输入昵称" />
        </el-form-item>
        <el-form-item label="性别" prop="touristSex">
          <el-select v-model="form.touristSex" placeholder="请选择性别">
            <el-option
              v-for="dict in dict.type.sys_user_sex"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="出生年份" prop="touristOld">
          <el-input v-model="form.touristOld" placeholder="请输入出生年份" />
        </el-form-item>
        <el-form-item label="类型" prop="touristType">
          <el-select v-model="form.touristType" placeholder="请选择类型">
            <el-option
              v-for="dict in dict.type.sys_tourist_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="来源" prop="touristArea">
          <el-select v-model="form.touristArea" placeholder="请选择来源">
            <el-option
              v-for="dict in dict.type.sys_tourist_area"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="爱好类型" prop="touristHobby">
          <el-checkbox-group v-model="form.touristHobby">
            <el-checkbox
              v-for="dict in dict.type.sys_tourist_hobby"
              :key="dict.value"
              :label="dict.value">
              {{dict.label}}
            </el-checkbox>
          </el-checkbox-group>
        </el-form-item>
        <el-form-item label="手机号码" prop="touristPhonenumber">
          <el-input v-model="form.touristPhonenumber" placeholder="请输入手机号码" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import * as echarts from 'echarts';
import { listTourist, getTourist, delTourist, addTourist, updateTourist } from "@/api/scenery/tourist";
export default {
  mounted() {
    setTimeout(() => {
      const myChart = echarts.init(this.$refs.Sex_ratio);
      // ... 其他ECharts相关代码 ...
    }, 100); // 适当调整延迟时间
    var sex_ratio = this.touristList.touristSex
    console.log(touristList)
    // console.log(typeof sex_ratio[0].touristSex)
    var data = []
    var man = 0
    var woman = 0
    // 循环遍历数组，取出数据，转成和data一样的格式
    for (var i = 0; i < sex_ratio.length; i++) {
      if (sex_ratio[i].touristSex == 1) {
        woman++
      } else {
        man++
      }
    }
    var man_data = {name: "男性", value: man}
    var woman_data = {name: "女性", value: woman}
    data.push(man_data)
    data.push(woman_data)
    data.push({
      value: sex_ratio.length,
      itemStyle: {
        // stop the chart from rendering this piece
        color: 'none',
        decal: {
          symbol: 'none'
        }
      },
      label: {
        show: false
      }
    })
    this.data = data
    // 画出图形
    this.Draw()
  },
  name: "Tourist",
  dicts: ['sys_tourist_hobby', 'sys_tourist_area', 'sys_user_sex', 'sys_tourist_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 游客管理表格数据
      touristList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        touristNickName: null,
        touristSex: null,
        touristOld: null,
        touristType: null,
        touristArea: null,
        touristHobby: null,
        touristPhonenumber: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询游客管理列表 */
    getList() {
      this.loading = true;
      listTourist(this.queryParams).then(response => {
        this.touristList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        touristId: null,
        touristNickName: null,
        touristSex: null,
        touristOld: null,
        touristType: null,
        touristArea: null,
        touristHobby: [],
        touristPhonenumber: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.touristId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加游客管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const touristId = row.touristId || this.ids
      getTourist(touristId).then(response => {
        this.form = response.data;
        this.form.touristHobby = this.form.touristHobby.split(",");
        this.open = true;
        this.title = "修改游客管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.touristHobby = this.form.touristHobby.join(",");
          if (this.form.touristId != null) {
            updateTourist(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTourist(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const touristIds = row.touristId || this.ids;
      this.$modal.confirm('是否确认删除游客管理编号为"' + touristIds + '"的数据项？').then(function() {
        return delTourist(touristIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('scenery/tourist/export', {
        ...this.queryParams
      }, `tourist_${new Date().getTime()}.xlsx`)
    },
    /** 画饼图 */
    Draw() {
      let myChart = this.$echarts.init(document.getElementById('Sex_ratio'))
      myChart.setOption({
        title: {
          text: '游客性别比例',
          x: 'center',
          y: '320px'
        },
        tooltip: {
          trigger: 'item'
        },
        legend: {
          top: '5%',
          left: 'center',
          selectedMode: false
        },
        series: [
          {
            name: '游客性别比例',
            type: 'pie',
            radius: ['40%', '70%'],
            center: ['50%', '70%'],
            // adjust the start angle
            startAngle: 180,
            label: {
              show: true,
              formatter(param) {
                // correct the percentage
                return param.name + ' (' + param.percent * 2 + '%)';
              }
            },
            data: this.data
          }
        ]
      })
    }
  }
};
</script>
